// pages/mine/find/vote/vote.js
var app = getApp()
import Countdown from '../../../../utils/wxCountdown.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [{
        id: 0,
        type: 'image',
      img: 'https://zkk-1256381563.cos.ap-shanghai.myqcloud.com/3.jpg',
      url:"/pages/news/detail/detail?url=https://mp.weixin.qq.com/s/1gC8rmkLl_hed7cGmbDmmA"
      },
      {
        id: 0,
        type: 'image',
        img: 'https://zkk-1256381563.cos.ap-shanghai.myqcloud.com/2.jpg',
        url: "/pages/news/detail/detail?url=https://mp.weixin.qq.com/s/7-Wo-_SiEOuOCo3sI0alaA"
      }, {
        id: 1,
        type: 'image',
        img: 'https://zkk-1256381563.cos.ap-shanghai.myqcloud.com/1.jpg',
        url: "/pages/news/detail/detail?url=https://mp.weixin.qq.com/s/1gC8rmkLl_hed7cGmbDmmA"
      }
    ],

    gridCol: 2,
    skin: false,
    collegeList: [],
    disable: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_show: false,
    is_load: true,
    vote_num: 0,
    isFold: true,
    is_end:false,
    can_vote_num:1
  },
  showAll: function(e) {

    this.setData({

      isFold: !this.data.isFold,

    })

  },
  time() {
    Countdown.init('2019-5-28 23:00:00', 'countdown1', this)
  },
  toAd(e){
    var that =this
    console.log(e.currentTarget.dataset.id)
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: that.data.swiperList[e.currentTarget.dataset.id].url,
    })
  },
  bindGetUserInfo(e) {
    var that = this;
    that.onLoad()
    if (e.detail.userInfo) {
      console.log(e.detail.userInfo.nickName)
      app.globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: app.globalData.url + 'user/code',
            data: {
              code: res.code,
              nick: e.detail.userInfo.nickName,
              avatar: e.detail.userInfo.avatarUrl,
            },
            method: "POST",
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              app.globalData.openid = res.data.data.openid //返回openid
              app.globalData.user_id = res.data.user_id
              console.log("------user_id------")
              console.log(res.data.user_id)
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */

  getUserVote() {
    var that = this
    that.setData({
      is_load: false
    })
    wx.request({
      url: app.globalData.url + 'college/' + app.globalData.user_id, // 仅为示例，并非真实的接口地址
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        var coll = that.data.collegeList

        if (res.data.vote_num < that.data.can_vote_num) {
          for (let i = 0; i < res.data.data.length; i++)
            coll[res.data.data[i] - 1].is_vote = true
        } else {
          for (let i = 0; i < coll.length; i++)
            coll[i].is_vote = true
        }
        that.setData({
          is_load: true,
          collegeList: coll,
          vote_num: res.data.vote_num
        })
      }
    })

  },
  getCollege() {
    var that = this
    that.setData({
      is_load: false
    })
    wx.request({
      url: app.globalData.url + 'college', // 仅为示例，并非真实的接口地址
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        let coll = res.data.data
        for (let i = 0; i < coll.length; i++) {
          coll[i].is_vote = false
        }
        that.setData({
          collegeList: coll,
          is_load: true
        })
      }
    })




  },
  onLoad: function(options) {
    this.time()
    this.getCollege()
    var that = this
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          wx.request({
            url: app.globalData.url + 'user/code/' + res.code,
            data: {},
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              console.log('----user-----')
              console.log(res.data.user_id)
              app.globalData.user_id = res.data.user_id
              that.getUserVote()
            },
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log(res.userInfo)
              that.setData({
                is_load: true
              })
            }
          })
          that.setData({
            is_show: false
          })
        } else {
          that.setData({
            is_load: true,
            is_show: true
          })
        }

      }
    })
  },
  toHold(e) {
    var that = this
    console.log(e.currentTarget.dataset.id)
    that.setData({
      is_load:false
    })
    wx.request({
      url: app.globalData.url + 'college', // 仅为示例，并非真实的接口地址
      data: {
        user_id: app.globalData.user_id,
        college_id: e.currentTarget.dataset.id.id
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log('----投票结果----')
        console.log(res.data)
        if (res.data.state == 1) {
          wx.showToast({
            title: '感谢支持^v^',
            icon: 'success',
            duration: 2000
          })
          var coll = that.data.collegeList
          console.log('------collegeList------')
          console.log(that.data.collegeList)
          console.log('------currentTarget------')
          console.log(e.currentTarget.dataset)
          coll[e.currentTarget.dataset.id.id - 1].num += 1
          
          if (that.data.vote_num < that.can_vote_num-1) {
            coll[e.currentTarget.dataset.id.id - 1].is_vote = true
          } else {
            for (let i = 0; i < coll.length; i++)
              coll[i].is_vote = true
          }
          that.setData({
            is_load: true,
            collegeList: coll,
            vote_num: that.data.vote_num + 1
          })

        }
      }
    })


  },
  toDetail(e) {

    let str = JSON.stringify(e.currentTarget.dataset.id);
    wx.navigateTo({

      url: './detail/detail?jsonStr=' + str,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})